/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __packet_add_skip.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/13 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __packet_add_skip(Frbyte_Packet *p_packet, unsigned int ui_buf_len);

static void case1(void)
{
    Frbyte_Packet *p_packet = NULL;
    int i_ret = 0;
    
    i_ret = __packet_add_skip(p_packet, 100);
    
    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
}

static void case2(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    
    p_packet = bytebuff_creat();
    
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 10;
    p_packet->ui_skip_len = 0;
    
    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //运行
    i_ret = __packet_add_skip(p_packet, 100);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"1234567890");
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,100);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    bytebuff_free(&p_packet);
    
}

static void case3(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    
    p_packet = bytebuff_creat();
    
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 10;
    p_packet->ui_skip_len = 0;
    
    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = NULL;
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //运行
    i_ret = __packet_add_skip(p_packet, 100);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,110);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    
    bytebuff_free(&p_packet);
    
}

static void case4(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    
    p_packet = bytebuff_creat();
    
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 15;
    p_packet->ui_skip_len = 0;
    
    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //添加第二个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    p_bytebuffer->p_buf = frstrdup("test2");
    p_bytebuffer->ui_len = 5;
    p_bytebuffer->pos = 0;
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //运行
    i_ret = __packet_add_skip(p_packet, 100);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"1234567890");
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,5);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"test2");
    }
    //确认第三个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 2);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,100);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    
    bytebuff_free(&p_packet);
    
}

static void case5(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    
    p_packet = bytebuff_creat();
    
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 15;
    p_packet->ui_skip_len = 0;
    
    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //添加第二个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = NULL;
    p_bytebuffer->ui_len = 5;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //运行
    i_ret = __packet_add_skip(p_packet, 100);
    
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"1234567890");
    }
    
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,105);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    
    bytebuff_free(&p_packet);
    
}

static void case6(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    
    p_packet = bytebuff_creat();
    
    p_packet->pos_index = 0;
    p_packet->ui_all_len = 10;
    p_packet->ui_skip_len = 0;
    
    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));
    
    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;
    
    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);
    
    //运行
    i_ret = __packet_add_skip(p_packet, 0);
    
    CU_ASSERT_EQUAL(i_ret,FR_FAIL);
    
    bytebuff_free(&p_packet);
}

int add_suite__packet_add_skip(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm __packet_add_skip()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
    CU_add_test(pSuite, "case5", case5);
    CU_add_test(pSuite, "case6", case6);

    return FR_SUCC;
}
